package com.group9.handinhand.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.group9.handinhand.dao.model.Community;
import com.group9.handinhand.dao.model.Event;
import com.group9.handinhand.dao.model.EventAttender;
import com.group9.handinhand.dao.model.EventPost;
import com.group9.handinhand.dao.model.User;
import com.group9.handinhand.service.so.UserTagSO;
/**
 * class for event dao
 *
 */
@Repository
public class EventDao extends BaseDaoImpl {

	public Event saveEvent(Event event) {
		save(event);
		return event;
	}
	/**
	 * finds event by tag
	 * @param tag
	 * @return
	 */
	public List<Event> findEventsByTag(UserTagSO tag) {
		String queryString = "Select distinct (et.event) from EventTag et inner join et.tag tt where tt.id=:tagId";
		return executeQueryString(queryString, "tagId", tag.getTag().getId());
	}
	/**
	 * finds event by tag name
	 * @param keyword
	 * @return
	 */
	public List<Event> findEventsByTagName(String keyword) {
		String queryString = "Select distinct (et.event) from EventTag et inner join et.tag tt where tt.tagName=:keyWord";
		return executeQueryString(queryString, "keyWord", keyword);
	}
	/**
	 * finds events by event name 
	 * @param keyword
	 * @return
	 */
	public List<Event> findEventsByName (String keyword) {
		String queryString = "from Event ev where str(ev.name) like :keyWord";
		return executeQueryString(queryString, "keyWord", "%"+keyword+"%");
	}
	/**
	 * finds events by community id
	 * @param communityId
	 * @return
	 */
	public List<Event> findEventsByCommunityId(Long communityId) {
		String queryString = "Select distinct (ev.event) from Community ev inner join ev.event cm where cm.id =:communityId";
		return executeQueryString(queryString, "communityId", communityId);
	}
	/**
	 * finds events by user id
	 * @param user
	 * @return
	 */
	public List<Event> findEventsByUser(User user) {
		String queryString = "Select distinct (ea.event) from EventAttender ea inner join ea.user es where es.id =:userId";
		return executeQueryString(queryString, "userId", user.getId());
	}
	/**
	 * finds event by id
	 * @param eventId
	 * @return
	 */
	public Event findEventById(Long eventId) {
		return (Event)findById(Event.class, eventId);
	}
	/**
	 * saves event post
	 * @param eventPost
	 * @return
	 */
	public EventPost saveEventPost(EventPost eventPost) {
		save(eventPost);
		return eventPost;
	}
	/**
	 * finds the attender of event by event id and the user id
	 * @param eventId
	 * @param userId
	 * @return
	 */
	public EventAttender findEventAttenderByEventIdAndUserId(Long eventId, Long userId) {
		String queryString = "from EventAttender as ea  where ea.user.id =:userId and ea.event.id =:eventId";
		return executeQueryStringForObject(queryString, new String[]{"userId" , "eventId"}, new Object[]{userId,eventId});
	}
	
	public EventAttender saveEventAttender(EventAttender eventAttender) {
		save(eventAttender);
		return eventAttender;
	}
	
	public void deleteEventAttender(EventAttender eventAttender) {
		eventAttender = findEntityByPropertyUnique(EventAttender.class, "id", eventAttender.getId());
		delete(eventAttender);
	}
	/**
	 * finds event by event id and user id
	 * @param eventId
	 * @param userId
	 * @return
	 */
	public Event findModeratedEventByEventIdAndUserId(Long eventId, Long userId) {
		String queryString = "Select e from Event e inner join e.moderators em where em.id =:userId and e.id =:eventId";
		return executeQueryStringForObject(queryString, new String[]{"eventId" , "userId"}, new Object[]{eventId,userId});
	}
	/**
	 * find moderators of event using event id
	 * @param eventId
	 * @return
	 */
	public List<User> findModeratorsByEventId(Long eventId) {
		String queryString = "Select em from Event e inner join e.moderators em where e.id =:eventId";
		return executeQueryString(queryString, "eventId", eventId);
	}

	public void updateEvent(Event event) {
		update(event);
	}

}
